{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "320b1071",
   "metadata": {},
   "source": [
    "## 字典结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7f99529c",
   "metadata": {},
   "outputs": [],
   "source": [
    "tang={}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "21e70c77",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(tang)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5574d193",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang=dict()\n",
    "type(tang)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "eef9814a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f358e45c",
   "metadata": {},
   "source": [
    "## 字典结构操作\n",
    "key - value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dd707faa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'first': 123}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang['first']=123\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "11f2467f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'first': 123, 'python': 456}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang['python']=456\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "93186c7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "456"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang['python']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "903ff596",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'first': 123, 'python': 789}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang['python']=789\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e8d2148f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'tang': 123, 'yu': 456, 'di': 789}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang={\"tang\":123,\"yu\":456,\"di\":789}\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6ef8269c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'tang': 123, 'yu': 456, 'di': 789}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8eb960b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'yudi': [1, 2, 3], 'yudi2': 4}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_value=[1,2,3]\n",
    "tang ={}\n",
    "tang[\"yudi\"]= tang_value\n",
    "tang[\"yudi2\"]=3\n",
    "tang[\"yudi2\"]=4\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "46e9a219",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'test': {'tang': 123, 'yudi': 456}, 'test2': {'tang2': 456, 'yudi2': 789}}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang={}\n",
    "d1={\"tang\":123,\"yudi\":456}\n",
    "d2={\"tang2\":456,\"yudi2\":789}\n",
    "tang['test']=d1\n",
    "tang['test2']=d2\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e098c50b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'tang': 123, 'yudi': 456}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang = dict([(\"tang\",123),('yudi',456)])\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "eafef114",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'tang': 124, 'yudi': 456}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang[\"tang\"] +=1\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "dc6c6359",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "124"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang.get(\"tang\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "4b8b9121",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'fengzi'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[18], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m tang[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfengzi\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n",
      "\u001b[1;31mKeyError\u001b[0m: 'fengzi'"
     ]
    }
   ],
   "source": [
    "tang[\"fengzi\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5dc1d7de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'meiyou'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang.get(\"fengzi\",\"meiyou\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ffce34da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'tang': 124, 'yudi': 456}"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "04725f9a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "124"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang.pop(\"tang\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "cb73f8ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'yudi': 456}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "79c4b452",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del tang[\"yudi\"]\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8f56bda7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'tang': 789, 'yudi': 456, 'python': 888}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang={\"tang\":123,\"yudi\":456}\n",
    "tang2={\"tang\":789,\"python\":888}\n",
    "tang.update(tang2)\n",
    "tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "e8beeb76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'tang' in tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "df6fbcfe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'hello' in tang"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ae6c225a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['tang', 'yudi', 'python'])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "822066e0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values([789, 456, 888])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "cf361825",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([('tang', 789), ('yudi', 456), ('python', 888)])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang.items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "327b9398",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
